$(function() {	
	

	$(".rule-text.ui-wizard-content[name=application]").autocomplete({
		source: function (request, response) {
			$.ajax({
				url: "/rules/get_json", type: "POST", dataType: "json",
				data: { startApp: request.term },
				success: function (data) {
					response($.map(data, function (item) {
						return {label:item.label, exe:item.exe}
						}));
					}
				});
			},
			select: function( event, ui ) {
				$(".rule-text.ui-wizard-content[name=application]").val(ui.item.label);
			}
		})
	});

	function restore_who(data){
		$("input[name=application]").val(data[0]["exe_label"]);
		$("input[name=window_title]").val(data[0]["window_title"]);
		$("input[name=url]").val(data[0]["url"]);
	}

	function restore_what(data){
		if (data[0]["personal_id"] != null){
			category_identifier = "select[name=personal] optgroup option[value = "+data[0]["personal_id"]+"]"
			radio_identifier = $("input[value=move_to_personal]")
			radio_identifier.attr('checked',true);
			category = $(category_identifier);
			category.attr('selected','selected');
		}
		else if (data[0]["project_id"] != null){
			category_identifier = "select[name=project] optgroup option[value = "+data[0]["project_id"]+"]"
			category = $(category_identifier);
			category.attr('selected','selected');
			radio_identifier = $("input[value=move_to_project]")
			radio_identifier.attr('checked',true);
		}
		else if (data[0]["delete_entry"] != null){
			radio_identifier = $("input[value=delete]")
			radio_identifier.attr('checked',true);
		}
	}

	function restore_when(data){
		scope = data[0]["scope"]
		if (scope == "all_the_time"){
			radio_identifier = $("input[value=all_the_time]")
			radio_identifier.attr('checked',true);
		}
		else if (scope == "before"){
			radio_identifier = $("input[value=before_date]")
			radio_identifier.attr('checked',true);
			$("input[name=before_date_field]").val(data[0]["before_date_field"]);
		}
		else if (scope == "after"){
			radio_identifier = $("input[value=after_date]")
			radio_identifier.attr('checked',true);
			$("input[name=after_date_field]").val(data[0]["after_date_field"]);
		}
		else if (scope == "range"){
			radio_identifier = $("input[value=date_range]")
			radio_identifier.attr('checked',true);
			$("input[name=daterange_end_date]").val(data[0]["daterange_end_date"]);
			$("input[name=daterange_start_date]").val(data[0]["daterange_start_date"]);
		}
	}

	function get_rule_info(rule_id){
		$.get("rules/get_rule_info", { "rule_id": rule_id },
		function(data){
			restore_who(data);
			restore_what(data);
			restore_when(data);
		});
	}



	function edit_rule_popup(rule_id){
		$('<input type = "hidden" name = "rule_id" value = '+rule_id+'>').insertAfter($("input[name=application]"));
		
		get_rule_info(rule_id);
		
		/*	if ($("#add_rule_form").formwizard("state").currentStep != "first"){
		$("#add_rule_form").formwizard("show","first");
		};
		$("button#next").html("Next Step &raquo;");
		$("#add_rule_modal form :text:visible:enabled:first").focus();
		$("#rule_start_datepicker").datepicker({ 
		dateFormat: 'D, M d, yy',
		showAnim: ''
		});

$("#rule_end_datepicker").datepicker({ 
dateFormat: 'D, M d, yy',
showAnim: ''
});

$("#rule_before_datepicker").datepicker({ 
dateFormat: 'D, M d, yy',
showAnim: ''
});

$("#rule_after_datepicker").datepicker({ 
dateFormat: 'D, M d, yy',
showAnim: ''
});

$("#rule_start_datepicker").datepicker("hide");
$("#rule_end_datepicker").datepicker("hide");
$("#rule_before_datepicker").datepicker("hide");
$("#rule_after_datepicker").datepicker("hide");
*/

}